from random import random
from tkinter import ttk
import tkinter as tk
from traceback import print_exc

from commonDecoratorSwitch.impl.WinDecrementLoseIncrementImpl import WinLoseDecoratorImpl
from commonModelData.impl.ModelData13N0 import ModelData13N0
from commonModelData.impl.ModelData13N1 import ModelData13N1
from commonModelData.impl.ModelData14N0 import ModelData14N0
from commonModelData.impl.ModelData14N1 import ModelData14N1
from commonModelData.impl.ModelData2N import ModelData2N
from commonModelData.impl.ModelData2N0 import ModelData2N0
from commonModelData.impl.ModelData2N1 import ModelData2N1
from commonServiceApi.impl.TtzTradeImpl import TTZTradeImpl
from constants import ColumnConstant
from pojo.BaseLogin import BLogin
from utils import MumberUtils, DataUtils
from utils.DataUtils import getHour
from utils.RedisUtils import RedisUtil

def dispalyIndexWindow(BLogin):
    indexWindow = tk.Tk()
    # startApplication(BLogin,indexWindow)
    # # 副窗口

    indexWindow.after(1000, lambda: startApplication(BLogin,indexWindow))
    # # 指定窗口大小
    # # window.geometry('400x200')
    # userId_label = tk.Label(indexWindow, text="用户id:")
    # userId_label.grid(row=0, column=0)
    # userIdText_label = tk.Label(indexWindow, text="测试用户")
    # userIdText_label.grid(row=0, column=1)
    # # 银珠数量 ,定时任务更新
    # yinzhu_label = tk.Label(indexWindow, text="银珠数量")
    # yinzhu_label.grid(row=1, column=0)
    #
    # yinzhuText_label = tk.Label(indexWindow, text="0")
    # yinzhuText_label.grid(row=1, column=1)
    # # 模式选择
    # model_labeltext = tk.Label(indexWindow, text="模式选择")
    # model_labeltext.grid(row=3, column=0)
    # # 模式下拉框取值
    # modelValues = ["2N0", "2N1", "3N0", "3N1", "3N2", "4N0", "4N1", "4N2", "4N3", "5N0", "5N1", "5N2", "5N3", "5N4"]
    # comboboxModel_var = tk.StringVar()
    # combobox = ttk.Combobox(indexWindow, values=modelValues, width=10, textvariable=comboboxModel_var)
    # combobox.grid(row=3, column=1)
    # # 盈利目标和盈利目标共用
    # tempValues = ["", "1w", "5w", "10w", "50w", "100w", "200w", "300w", "400w", "500w", "600w", "700w", "800w",
    #               "900w",
    #               "100w", "1100w", "1200w", "1300w", "1400w", "1500w", "1600w", "1700w", "1800w", "4000w", "5000w",
    #               "6000w", "7000w", "8000w", "9000w", "10000w"]
    # # 盈利目标标签
    # win_label = tk.Label(indexWindow, text="盈利目标")
    # win_label.grid(row=4, column=0)
    # # 盈利目标设置
    # winValues = tempValues
    # comboboxWin_var = tk.StringVar()
    # combobox = ttk.Combobox(indexWindow, values=winValues, width=10, textvariable=comboboxWin_var)
    # combobox.grid(row=4, column=1)
    # # 亏损目标标签
    # cost_label = tk.Label(indexWindow, text="亏损目标")
    # cost_label.grid(row=5, column=0)
    # # 亏损目标设置
    # costValues = tempValues
    # comboboxCost_var = tk.StringVar()
    # combobox = ttk.Combobox(indexWindow, values=costValues, width=10, textvariable=comboboxCost_var)
    # combobox.grid(row=5, column=1)
    # # 滚雪球轮数设置
    # Snowball_label = tk.Label(indexWindow, text="雪球模式设置")
    # Snowball_label.grid(row=6, column=0)
    # # 雪球模式轮数下拉框取值
    # SnowballValues = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
    #                   "18",
    #                   "19", "20", "21", "22", "23"]
    # comboboxSnowball_var = tk.StringVar()
    # combobox = ttk.Combobox(indexWindow, values=SnowballValues, width=10, textvariable=comboboxSnowball_var)
    # combobox.grid(row=6, column=1)
    # # 滚雪球每期目标设置
    # snowball_label = tk.Label(indexWindow, text="每期目标")
    # snowball_label.grid(row=7, column=0)
    # values = ["0.5w", "1w", "2w", "5w", "10w", "20w", "30w", "40w", "50w", "60w", "70w", "80w", "90w", "100w",
    #           "110w",
    #           "120w", "130w",
    #           "140w", "150w", "160w", "170w", "180w", "190w", "200w", "210w", "220w", "230w", "240w",
    #           "250w", "260w", "270w", "280w",
    #           "290w", "300w", "310w"
    #           ]
    # comboboxSnowballtarget_var = tk.StringVar()
    # combobox = ttk.Combobox(indexWindow, values=values, width=10, textvariable=comboboxSnowballtarget_var)
    # combobox.grid(row=7, column=1)
    # # 北京时间
    # time_label = tk.Label(indexWindow, text="时间显示在这里")
    # time_label.grid(row=3, column=3)
    # # 剩余下注时间
    # surplus_time_label = tk.Label(indexWindow, text="剩余下注时间")
    # surplus_time_label2 = tk.Label(indexWindow, text="")
    # surplus_time_label.grid(row=4, column=3)
    # surplus_time_label2.grid(row=4, column=4)
    #
    # # 下注方案显示
    # model_label = tk.Label(indexWindow, text="下注方案显示在这里")
    # model_label.grid(row=5, column=3)
    # # 盈利目标显示
    # win_label = tk.Label(indexWindow, text="盈利目标")
    # win_label.grid(row=6, column=3)
    # # 创建Treeview 控件 ,Execl表格组件
    #
    # treeview = ttk.Treeview(indexWindow)
    # treeview["columns"] = ("期号", "号码", "奖池", "下注时间", "下注金额", "是否下注", "中奖金额")
    # # 设置列的标题
    # treeview.heading("期号", text="期号")
    # treeview.heading("号码", text="号码")
    # treeview.heading("奖池", text="奖池")
    # treeview.heading("下注时间", text="下注时间")
    # treeview.heading("下注金额", text="下注金额")
    # treeview.heading("是否下注", text="是否下注")
    # treeview.heading("中奖金额", text="中奖金额")
    # # 显示表格
    # treeview.grid(row=25, columnspan=10)
    #
    # indexWindow.mainloop()


def startApplication(BLogin, indexWindow):

    try:
        second = DataUtils.getSecondDistince(90)
        # print(f"当前时间：{second} s")
        if second < 40 and second > 38:
            trade = TTZTradeImpl(BLogin)
            BLogin = readUserInfoFromRedis(BLogin)
            # 检查是否启动
            # if MumberUtils.strToInt(BLogin.startHour) <= MumberUtils.strToInt(getHour()) \
            #         and MumberUtils.strToInt(getHour()) <=  MumberUtils.strToInt(BLogin.endHour)\
            #         and BLogin.todayIsStart:
            #     print("命中的下注时间段")

            model = getModelFromBlogin(BLogin)
            trade.setADecoratorSwitch(WinLoseDecoratorImpl(model, BLogin))
            trade.makeTrade()
        indexWindow.after(1000, lambda: startApplication(BLogin,indexWindow))
    except:
        print_exc()


def getModelFromBlogin(BLogin):
    model = None
    if BLogin.model.find("2N")!=-1:
        model = ModelData2N()
    elif BLogin.model == "2N0":
    # if BLogin.model == "2N0":
        model = ModelData2N0()
    elif BLogin.model == "2N1":
        model = ModelData2N1()
    elif BLogin.model == "13-0":
        model = ModelData13N0()
    elif BLogin.model == "13-1":
        model = ModelData13N1()
    elif BLogin.model == "14-0":
        model = ModelData14N0()
    elif BLogin.model == "14-1":
        model = ModelData14N1()

    # 设置BLogin
    if model is not None:
        model.setBlogin(BLogin=BLogin)
    return model


def readUserInfoFromRedis(BLogin):
    redis = RedisUtil()
    # 今日是否启动
    BLogin.todayIsStart = redis.hget(BLogin.userPhone,ColumnConstant.TODAY_IS_START)

    # 启动时间
    BLogin.startHour = redis.hget(BLogin.userPhone,ColumnConstant.START_HOUR)
    # 停止时间
    BLogin.endHour = redis.hget(BLogin.userPhone,ColumnConstant.END_HOUR)
    # 今日盈利目标
    BLogin.todayWinAmount = redis.hget(BLogin.userPhone,ColumnConstant.TODAY_WIN_SUM)
    # 今日亏损目标
    BLogin.todayCostAmount = redis.hget(BLogin.userPhone,ColumnConstant.TODAY_COST_SUM)
    # 是否交换卡片
    BLogin.exchangeIsCard = redis.hget(BLogin.userPhone,ColumnConstant.EXCHANGE_IS_CARD)
    # 交换卡片次数
    BLogin.exchangeCardNum = redis.hget(BLogin.userPhone,ColumnConstant.EXCHANGE_CARD_NUM)
    # 交换卡片频率,1d1次,3d1次,5d一次
    BLogin.exchangeCardFrequency = redis.hget(BLogin.userPhone,ColumnConstant.EXCHANGE_CARD_FREQUENCY)

    # 模式名称
    BLogin.model = redis.hget(BLogin.userPhone,ColumnConstant.USER_MODE)
    # 用户类型
    BLogin.userType = redis.hget(BLogin.userPhone,ColumnConstant.USER_TYPE)
    # 用户通知列表
    BLogin.userNoticeList = redis.hget(BLogin.userPhone,ColumnConstant.USER_NOTICE_LIST)
    return BLogin